package com.wangbl.infomanager;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.tangye.android.http.Request.ResponseListener;
import com.wangbl.infomanager.bean.CatalogDetail;
import com.wangbl.infomanager.bean.CatalogListWrap;
import com.wangbl.infomanager.http.Http;
import com.wangbl.infomanager.http.Response;
import com.wangbl.infomanager.utils.Logger;
import com.wangbl.infomanager.view.XListView;
import com.wangbl.infomanager.view.XListView.OnXListViewListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CatalogDetailActivity extends BaseActivity implements
		OnXListViewListener, OnItemClickListener {

	protected static final String TAG = "CatalogDetailActivity";
	private XListView listView;
	private int num = 15;
	private int page = 1;
	private String id;
	private String title;
	private List<CatalogDetail> details;
	private DetailAdapter adapter;
	private Http http;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_catalog_detail);
		super.onCreate(savedInstanceState);
		details = new ArrayList<CatalogDetail>();
		adapter = new DetailAdapter();

		Intent intent = getIntent();
		id = intent.getStringExtra("id");
		title = intent.getStringExtra("title");
		listView = (XListView) findViewById(R.id.listview_catalog_detail);

		listView.setFooterReady(true);
		listView.setPullRefreshEnable(true);
		listView.setPullLoadEnable(1);
		listView.setAdapter(adapter);
		listView.setXListViewListener(this);
		listView.setOnItemClickListener(this);
		listView.refresh(this);
		
		title_text.setText(title);
		title_user.setVisibility(View.GONE);
		title_menu.setVisibility(View.GONE);
		title_back.setVisibility(View.VISIBLE);
//		doRequest();
	}

	private void doRequest(final boolean isLoadMore) {
		if(lock){
			Logger.e(TAG, "此方法已锁...");
			return;
		}
		lock = true;
		if(!isLoadMore){
			page = 1;
		}
		http = new Http("/rucjson/title.php", true);
		http.setParam("page", page + "");
		http.setParam("num", num + "");
		http.setParam("id", id);
		http.setDebug(Http.isDebug);
		http.asyncRequest(new ResponseListener<Response>() {

			@Override
			public void onResp(int arg0, Response arg1, Object... arg2) {
				Logger.i(TAG, arg1.json);
				lock = false;
				List<CatalogDetail> list = parser(arg1.json);
				if (list != null) {
					if(isLoadMore){
						details.addAll(list);
						adapter.notifyDataSetChanged();
						page++;
					} else {
						details.clear();
						details.addAll(list);
						adapter.notifyDataSetChanged();
						page++;
					}
				} else {
					toast("没有更多数据...");
				}
				
				if(isLoadMore){
					listView.stopLoadMore();
				} else {
					listView.stopRefresh();
				}
				listView.stopLoadMore();
			}

			@Override
			public void onErr(int arg0, String arg1, int arg2, Object... arg3) {
				lock = false;
				if(isLoadMore){
					listView.stopLoadMore();
				} else {
					listView.stopRefresh();
				}
				listView.stopLoadMore();
				toast(arg1);
			}
		});
	}

	private class DetailAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return details.size();
		}

		@Override
		public Object getItem(int position) {
			return details.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			Holder holder;
			CatalogDetail detail = details.get(position);
			if (convertView != null) {
				view = convertView;
				holder = (Holder) view.getTag();
			} else {
				view = View.inflate(CatalogDetailActivity.this,
						R.layout.item_listview_catalog_detail, null);
				holder = new Holder();
				holder.title = (TextView) view
						.findViewById(R.id.tv_catalog_detail_title);
				holder.subTitle = (TextView) view
						.findViewById(R.id.tv_catalog_detail_subtitle);
				holder.date = (TextView) view
						.findViewById(R.id.tv_catalog_detail_date);
				view.setTag(holder);
			}

			holder.title.setText(detail.getTitle());
			holder.subTitle.setText(title);
			holder.date.setText(detail.getDatetime());
			return view;
		}

	}

	class Holder {
		TextView title;
		TextView subTitle;
		TextView date;
	}

	private List<CatalogDetail> parser(String json) {
		Gson gson = new Gson();
		CatalogListWrap wrap = gson.fromJson(json, CatalogListWrap.class);
		return wrap.getList();
	}
	
	private volatile boolean lock;
	@Override
	public void onRefresh() {
		Logger.i(TAG, "onRefresh");
		doRequest(false);
	}

	@Override
	public void onLoadMore() {
		Logger.i(TAG, "onLoadMore");
		doRequest(true);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Logger.i(TAG, "position:"+position+"   "+details.size());
		CatalogDetail detail = details.get(position-1);
		if(detail.getId() == null){
			toast("此项没有详情...");
			return;
		}
		String url = "http://irmsma.ruc.edu.cn/dis.php?id="+detail.getId()+"&type=aaa";
		Intent intent = new Intent(this,InfoDetailActivity.class);
		intent.putExtra("url", url);
		startActivity(intent);
	}

	@Override
	protected void interrupt() {
		if(http != null){
			http.abort();
		}
		
	}

}
